<?php
/**
 * Created by glq.
 * Date: 2018/10/21
 * Time: 10:48
 */

namespace Label\Controller;

use Common\Controller\AdminbaseController;

class AdminIndexController extends AdminbaseController{

    protected $label_model;
    protected $label_type_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size            = 20;
        $this->label_model          = D("Label/Label");
        $this->label_type_model     = D("Label/LabelType");
    }

    // 标签类型列表
    public function label_type() {
        if ($name = I('post.name')) {
            $where['name'] = array('like', "%$name%");
            $this->assign('name', $name);
        }

        $label_types = $this->label_type_model->where($where)->order('id desc')->select();

        $this->assign('label_types', $label_types);

        $this->display();
    }


    // 编辑标签分类
    public function label_type_edit()
    {
        if (IS_POST) {
            $id     = I("post.id", 0, 'intval');
            $data   = array(
                'name'      => I('name'),
                'status'    => I('status'),
            );

            if (!pre_data($data)) {
                $this->error('请完善必要信息');
            }

            $where['name'] = I('name');
            if ($id) {
                $where['id'] = ['not in', [$id]];
            }
            $has_label_type = $this->label_type_model->where($where)->find();
            if ($has_label_type) {
                $this->error('类型名称重复');
            }

            if ($id) {
                $this->label_type_model->where(['id' => $id])->save($data);
                $this->success('修改成功');

            } else {
                $re = $this->label_type_model->add($data);

                if ($re) {
                    $this->success('创建成功');
                } else {
                    $this->error('创建失败');
                }
            }
        } else {
            $id     = I("get.id", 0, 'intval');
            $info   = $this->label_type_model->where(['id' => $id])->find();
            if ($id) {
                if ($info) {

                    if (I("get.delete", 0, 'intval')) {

                        $has_label = $this->label_model->where(['label_type_id' => $id])->find();
                        if ($has_label) {
                            $this->error("该分类下还有标签");
                        }

                        $re = $this->label_type_model->where(['id' => $id])->delete();

                        if ($re) {
                            $this->success('删除成功');
                        } else {
                            $this->error('删除失败');
                        }

                    } else {

                        $this->assign('info', $info);
                        $this->display();
                    }

                } else {
                    $this->error('数据错误或已删除');
                }
            } else {
                $this->display();
            }
        }
    }


    // 标签列表
    public function label_list() {

        if ($abbr = I('abbr')) {
            $where['abbr'] = $abbr;
            $this->assign('abbr', $abbr);
        }

        // 标签分类列表
        $label_type_lists = $this->label_type_model->order('id desc')->select();
        $this->assign('label_type_lists', $label_type_lists);

        if ($label_type_id = I('label_type_id')) {
            $where['label_type_id'] = $label_type_id;
            $this->assign('label_type_id', $label_type_id);
        } else {
            if ($label_type_lists) {
                $where['label_type_id'] = array('in', _array_column($label_type_lists, 'id'));
            }
        }
        // 标签列表
        $label_lists = $this->label_model->order('id desc')->where($where)->select();
        $this->assign('label_lists', $label_lists);

        if (IS_AJAX) {
            $data = array(
                'status' => 1,
                'data'   => $label_lists,
            );
            $this->ajaxReturn($data);
        } else {
            $this->display();
        }
    }


    // 编辑标签
    public function label_edit()
    {
        if (IS_POST) {
            $id     = I("post.id", 0, 'intval');
            $data   = array(
                'label_type_id'     => I('label_type_id'),
                'name'              => I('name'),
                'desc'              => I('desc'),
            );

            if (!pre_data($data, ['desc'])) {
                $this->error('请完善必要信息');
            }

            if (empty($data['label_type_id'])) {
                $this->error('请完善必要信息');
            }

            $where['name'] = I('name');
            if ($id) {
                $where['id'] = ['not in', [$id]];
            }
            $has_label = $this->label_model->where($where)->find();
            if ($has_label) {
                $this->error('标签名称重复');
            }


            $data['abbr'] = getFirstCharter($data['name']);

            if ($id) {
                $this->label_model->where(['id' => $id])->save($data);
                $this->success('修改成功');

            } else {
                $re = $this->label_model->add($data);

                if ($re) {
                    $this->success('创建成功');
                } else {
                    $this->error('创建失败');
                }
            }
        } else {
            $id     = I("get.id", 0, 'intval');
            $info   = $this->label_model->where(['id' => $id])->find();

            // 标签类型
            $label_types = $this->label_type_model->order('id desc')->select();
            $this->assign('label_types', $label_types);

            if ($id) {
                if ($info) {

                    if (I("get.delete", 0, 'intval')) {
                        $re = $this->label_model->where(['id' => $id])->delete();

                        if ($re) {
                            $this->success('删除成功');
                        } else {
                            $this->error('删除失败');
                        }

                    } else {
                        $this->assign('info', $info);
                        $this->display();
                    }

                } else {
                    $this->error('数据错误或已删除');
                }
            } else {
                $this->display();
            }
        }
    }

    // 供其他模块选择标签使用
    public function select_label()
    {
        // 已选择的标签id
        $has_labels = I('has_labels');
        $this->assign('has_labels', $has_labels);
        $has_labels = explode(',', $has_labels);

        // 标签分类列表
        $label_type_lists = $this->label_type_model->order('id asc')->select();
        $this->assign('label_type_lists', $label_type_lists);

        // 搜索条件
        if ($label_type_id = I('label_type_id')) {
            $where['label_type_id'] = $label_type_id;
            $this->assign('label_type_id', $label_type_id);
        } else {
            if ($label_type_lists) {
                $where['label_type_id'] = array('in', _array_column($label_type_lists, 'id'));
            }
        }

        if ($abbr = I('abbr')) {
            $where['abbr'] = $abbr;
            $this->assign('abbr', $abbr);
        }
        // 标签列表
        $label_lists = $this->label_model->order('id desc')->where($where)->select();
        foreach ($label_lists as $k => $v) {
            $v['checked'] = "";
            if (in_array($v['id'], $has_labels)) {
                $v['checked'] = "checked";
            }
            $label_lists[$k] = $v;
        }
        $this->assign('label_lists', $label_lists);

        if (IS_AJAX) {
            $data = array(
                'status' => 1,
                'data'   => $label_lists,
            );
            $this->ajaxReturn($data);
        } else {
            $this->display();
        }
    }

    public function get_label_name()
    {
        $label_ids  = I('label_ids');
        $label_ids  = array_filter(explode(',', $label_ids));
        if (empty($label_ids)) {
            $labels     = [];
        } else {
            $labels     = $this->label_model->where(['id' => ['in', $label_ids]])->field('id, name')->order('id asc')->select();
        }

        $this->ajaxReturn(['status' => 1, 'data' => $labels]);
    }
}